
#include "hal_mcu.h"
#include "osal.h"
#include "stdio.h"
#include "string.h"


#define x_malloc  osal_mem_alloc


//uint8 lm_util_get_xor(uint8 *ptr, uint8 len)
//{ 
//	uint8 xorvalue = 0;

//	xorvalue = *ptr;
//	
//	for(uint8 i = 1; i < len; i++) {
//		xorvalue ^= *(ptr + i);
//	}
//	
//	return xorvalue;
//}

//bool is_all_ff(uint8* buf, uint8 len)
//{
//    for(int i = 0; i < len; i++)
//    {
//        if ( 0xff != buf[i])
//        {
//            return false;
//        }
//    }
//    return true;
//}


char *hex_2_string(const unsigned char hex[], int length)
{
    size_t len;
    char *dump;
    const char hex2str[] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
    char *p = dump;
    int i;
    len = length * 2 + 1;

    if ((dump = (char *)x_malloc(len)) == 0) {
        return (char*)0;
    }

    

    
    
    for( i=0; i<length; i++ ){
      *p++ = hex2str[ (hex[i] >> 4) & 0xF ];
      *p++ = hex2str[ hex[i] & 0xF ];
    }

    *p = 0;

    return dump;
}

/* sting of hex  -> hex*/
/* forex: "1816" -> 0x18 0x16*/
bool StringToHex(char *str, unsigned char *strhex)
{	
	uint8 i,cnt=0;	
	char *p = str;             
	uint8 len = strlen(str); 	
	
	
	if((len %2) != 0) {
	    //WARNING(" StringToHex Length ERROR \n");
		return false;
	}
	
	for(i = 0; (i < len -1) && (p != NULL) ; i += 2) {
        sscanf( p, "%2x", (uint32 *)&strhex[cnt++]);
		p+=2;
	}	

	strhex[cnt] = '\0';
	
	return true;
}



/*********************************************************************
 * @fn      _itoa
 *
 * @brief   convert a 16bit number to ASCII
 *
 * @param   num -
 *          buf -
 *          radix -
 *
 * @return  void
 *
 *********************************************************************/
void _itoa(uint16 num, uint8 *buf, uint8 radix)
{
  char c,i;
  uint8 *p, rst[5];

  p = rst;
  for ( i=0; i<5; i++,p++ )
  {
    c = num % radix;  // Isolate a digit
    *p = c + (( c < 10 ) ? '0' : '7');  // Convert to Ascii
    num /= radix;
    if ( !num )
      break;
  }

  for ( c=0 ; c<=i; c++ )
    *buf++ = *p--;  // Reverse character order

  *buf = '\0';
}


#if 0
#define CRC16 0x8005

uint16 gen_crc16(const uint8 *data, uint16 size)
{
    uint16 out = 0;
    int bits_read = 0, bit_flag;
    uint16 out_big_end;

    /* Sanity check: */
    if(data == NULL)
        return 0;

    while(size > 0)
    {
        bit_flag = out >> 15;

        /* Get next bit: */
        out <<= 1;
        out |= (*data >> (7 - bits_read)) & 1;

        /* Increment bit counter: */
        bits_read++;
        if(bits_read > 7)
        {
            bits_read = 0;
            data++;
            size--;
        }

        /* Cycle check: */
        if(bit_flag)
            out ^= CRC16;

    }

    out_big_end = (out << 8) | (out >> 8);
    return out_big_end;
}
#endif


void InvertUint8(uint8 *DesBuf, uint8 *SrcBuf)
{
    int i;
    uint8 temp = 0;

    for(i = 0; i < 8; i++)
    {
        if(SrcBuf[0] & (1 << i))
        {
            temp |= 1<<(7-i);
        }
    }
    DesBuf[0] = temp;
}


void InvertUint16(uint16 *DesBuf, uint16 *SrcBuf)
{
    int i;
    uint16 temp = 0;

    for(i = 0; i < 16; i++)
    {
        if(SrcBuf[0] & (1 << i))
        {
            temp |= 1<<(15 - i);
        }
    }
    DesBuf[0] = temp;
}


uint16 lm_crc_x25(uint8 *puchMsg, uint16 usDataLen)
{
    uint16 wCRCin = 0xFFFF;
    uint16 wCPoly = 0x1021;
    uint8 wChar = 0;
    uint16 i;

    while (usDataLen--)
    {
        wChar = *(puchMsg++);
        InvertUint8(&wChar, &wChar);
        wCRCin ^= (wChar << 8);

        for(i = 0;i < 8;i++)
        {
            if(wCRCin & 0x8000)
            {
                wCRCin = (wCRCin << 1) ^ wCPoly;
            }
            else
            {
                wCRCin = wCRCin << 1;
            }
        }
    }
    InvertUint16(&wCRCin, &wCRCin);
    return (wCRCin^0xFFFF) ;
}




